import 'package:inmotion_pro/l10n/local.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'controller.dart';

// 查看日志界面
class LogsViewPage extends BaseGetView<LogsController> {
  LogsViewPage({super.key});

  @override
  String get title => local.app_logs_nav_title;

  @override
  Widget buildBody(BuildContext context) {
    return Obx(
      () => Container(
        color: Colors.black,
        height: double.infinity,
        width: double.infinity,
        child: ListView.builder(
          itemCount: controller.logList.length,
          itemBuilder: (context, index) {
            final log = controller.logList[index];
            return Material(
              child: ListTile(
                splashColor: LxCol.backgroundColorContainerActive,
                contentPadding: const EdgeInsets.symmetric(horizontal: 16),
                leading: CircleAvatar(
                  backgroundColor: _getLogColor(log.level),
                  child: Text(
                    log.level.substring(0, 1).toUpperCase(),
                    style: const TextStyle(color: Colors.white),
                  ),
                ),
                title: Text(
                  log.message,
                ),
                subtitle: Text('${log.timestamp} '
                    '[${log.level}]${log.tag != null ? ' #${log.tag}' : ''}'),
                textColor: _getLogColor(log.level),
              ),
            );
          },
        ),
      ),
    );
  }

  Color? _getLogColor(String level) {
    debugPrint('Log level: $level');
    switch (level) {
      case 'ERROR':
        return Colors.red;
      case 'WARNING':
        return Colors.orange;
      case 'INFO':
        return Colors.grey;
      case 'DEBUG':
        return Colors.green;
      default:
        return null;
    }
  }
}
